from PySide6.QtWidgets import *

from widgets.scriptWidgets.ScriptWidget import ScriptWidget
from widgets.ScriptContent import ScriptContent
from PySide6.QtCore import QRect, Qt
from config import ScriptList


class CreateWindow(QMainWindow):
    def __init__(self, parent):
        super(CreateWindow, self).__init__(parent)
        self.setWindowModality(Qt.WindowModality.ApplicationModal)
        self.setWindowTitle("新建脚本")
        self.setObjectName("CreateWindow")
        self.resize(500, 680)
        self.initUI()
        self.show()

    def initUI(self):
        self.centralwidget = QWidget(self)
        self.centralwidget.setObjectName(u"centralwidget")

        self.labNameTitle = QLabel(self.centralwidget)
        self.labNameTitle.setObjectName(u"labNameTitle")
        self.labNameTitle.setGeometry(QRect(27, 10, 61, 20))
        self.labNameTitle.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
        self.labNameTitle.setText("脚本名:")

        self.editName = QLineEdit(self.centralwidget)
        self.editName.setObjectName(u"editName")
        self.editName.setGeometry(QRect(100, 10, 181, 21))
        self.editName.setMaxLength(16)
        self.editName.setPlaceholderText('脚本名')

        self.labDescription = QLabel(self.centralwidget)
        self.labDescription.setObjectName(u"labDescription")
        self.labDescription.setGeometry(QRect(7, 40, 81, 20))
        self.labDescription.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
        self.labDescription.setText("脚本描述:")

        self.editDescription = QLineEdit(self.centralwidget)
        self.editDescription.setObjectName(u"editDescription")
        self.editDescription.setGeometry(QRect(100, 40, 181, 21))
        self.editDescription.setMaxLength(16)
        self.editDescription.setPlaceholderText('脚本描述')

        self.labScript = QLabel(self.centralwidget)
        self.labScript.setObjectName(u"labScript")
        self.labScript.setGeometry(QRect(30, 70, 58, 16))
        self.labScript.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
        self.labScript.setText("脚本:")

        self.scrollScript = QScrollArea(self.centralwidget)
        self.scrollScript.setObjectName(u"scrollScript")
        self.scrollScript.setGeometry(QRect(30, 100, 450, 511))
        self.scrollScript.setWidgetResizable(True)

        self.scriptContent = ScriptContent(initType='create')
        self.scriptContent.setGeometry(QRect(0, 0, 448, 509))
        self.scrollScript.setWidget(self.scriptContent)

        self.btnSave = QPushButton(self)
        self.btnSave.setObjectName(u"btnSave")
        self.btnSave.setGeometry(QRect(270, 630, 100, 32))
        self.btnSave.setText("保存")
        self.btnSave.clicked.connect(self.save)

        self.btnCancel = QPushButton(self)
        self.btnCancel.setObjectName(u"btnCancel")
        self.btnCancel.setGeometry(QRect(100, 630, 100, 32))
        self.btnCancel.setText("取消")
        self.btnCancel.clicked.connect(self.cancel)

        self.setCentralWidget(self.centralwidget)

    def cancel(self):
        result = QMessageBox.question(self, '取消', '确认将不会保存脚本，确认取消?')
        if result == QMessageBox.Yes:
            self.close()
            self.deleteLater()

    def save(self):
        name = self.editName.text()
        description = self.editDescription.text()

        if len(name) == 0 or len(description) == 0:
            QMessageBox.warning(self, '保存', '保存失败，请填写脚本名和描述')
            return

        widgets = self.scrollScript.widget().children()
        script = {
            "name": self.editName.text(),
            "content": [],
            "description": self.editDescription.text()
        }

        for widget in widgets:
            if not isinstance(widget, ScriptWidget):
                continue

            widgetData = widget.data
            if 'error' in widgetData.keys():
                QMessageBox.information(self, '保存', '保存失败' + widgetData['error'])
                return
            script['content'].append(widgetData)

        ScriptList.add(script)
        QMessageBox.information(self, '保存', '保存成功')
        self.close()
        self.deleteLater()

    def closeEvent(self, event):
        if self.parent()._createWindow is not None:
            self.parent()._createWindow = None
